package com.yihenf.intent;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private final static int REQUEST_CODE = 1;
	Button MainBtn = null;
	Button SendSmsBtn = null;
	Button ToSecondBtn = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		MainBtn = (Button)findViewById(R.id.mainbtn);
		MainBtn.setOnClickListener(BtnOnClickListener);
		
		SendSmsBtn = (Button)findViewById(R.id.smsbtn);
		SendSmsBtn.setOnClickListener(BtnOnClickListener);
		
		ToSecondBtn = (Button)findViewById(R.id.to_second);
		ToSecondBtn.setOnClickListener(BtnOnClickListener);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	
	private OnClickListener BtnOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			
			switch (v.getId()) {
			case R.id.mainbtn:
				intent.setAction(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:18674025890"));
				startActivity(intent);
				break;
			case R.id.smsbtn:
				intent.setAction(Intent.ACTION_SENDTO);
				intent.setData(Uri.parse("smsto:5554"));
				intent.putExtra("sms_body","wellcome to...");
				startActivity(intent);
				break;
			case R.id.to_second:
				intent.setClass(MainActivity.this, SecondActivity.class);
				intent.putExtra("str", "intent deom");
				startActivityForResult(intent, REQUEST_CODE);
				break;
			default:
				break;
			}
			
			
		}
	};
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(REQUEST_CODE == requestCode)
		{
			if(resultCode == SecondActivity.RESULT_CODE)
			{
				Bundle bundle = data.getExtras();
				String str = bundle.getString("back");
				Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	

}
